#!/usr/bin/python
'''
Created by Colin Gillespie <c.gillespie@ncl.ac.uk>
$LastChangedDate$

Class are set in VariableDict.output
Each ConvertTo class should have an argument 'convert' and also perhaps getDifferential

The getOutput function dispatches classes depending on global values.

'''

__revision__ = '$Rev$'

from GlobalVariables import globalVariables
from sympy import Symbol, diff, sympify
from GetReactionInformation import ReactionInformation
from mrange import cumRange


class ConvertToMaple(object):
    
    def convert(self, values):
        if sum(values) == 0:
            maple_value = 1
        elif sum(values) == 1:
            idx = list(values).index(1)
            r = ReactionInformation()
            sp = r.species_order[idx]
            mu = '%s(t)' % sp
            maple_value = sympify(mu)
        elif (sum(values), max(values)) == (2,2):
            idx = list(values).index(2)
            sp = ReactionInformation().species_order[idx]
            mu = 'E_%s_%s(t)' % (sp, sp)
            maple_value = sympify(mu)
        elif (sum(values), max(values)) == (2,1):
            idx1 = list(values).index(1)
            idx2 = idx1 + 1 + list(values)[idx1+1:].index(1)
            sp1 = ReactionInformation().species_order[idx1]
            sp2 = ReactionInformation().species_order[idx2]            
            mu = 'E_%s_%s(t)' % (sp1, sp2)
            maple_value = sympify(mu)
        else:
            inner = ''
            for v in values:
                inner += str(v)
                mu = 'mu_%s(t)' % inner
            maple_value = sympify(mu)

        return maple_value 

#def ConvertToMapleAsCumulant(object):
#    ''
#    def convert(self, values):
#        if sum(values) == 0:
#            self.cum_vars[values] = 0
#        elif sum(values) == 1:
#            idx = list(values).index(1)
#            
#            sp = ReactionInformation().species_order[idx]
#            mu = '%s(t)' % sp
#XXXX            self.mom_vars[values] = Symbol(mu)
#            
#        else:
#            inner = ''
#            for v in values:
#                inner += str(v)
#                kappa = 'kappa_%s(t)' % inner
#            self.cum_vars[values] = Symbol(kappa)
#        return self.cum_vars[values]

class ConvertToGSL(object):

    def __init__(self, order={}):
        self.order = {}
        r = ReactionInformation()
       
        max_values = [globalVariables()['closure_order']] * len(r.species_order)
#        max_values = [3,3,3]

        for i, tup in enumerate(cumRange(max_values)):
            self.order[tup] = i
#        print self.order

    def convert(self, values):
        if sum(values) == 0:
            moment = 1          
        else:
            moment = Symbol('EX[%s]' % self.order[values])

        return moment
    
    def getDifferential(self, values):
        diff = 'dEX[%s]' % self.order[values]
        return diff

def getOutput():
    command = 'ConvertTo' + globalVariables()['output_format']
    try:
        value = eval(command)
        return value()
    except NameError:
        raise 'Error: Unknown output - %s' % command
        
if __name__ == '__main__':
    ConvertToGSL()
